<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Master_pertanyaan extends arfaController {

	public function __construct(){
		parent::__construct();
		$this->load->helper('general');
		$this->cekLogin();
		$this->load->model("m_tanya_model");
	}
	public function index()
	{
		$data["master_tanya"]	= $this->m_tanya_model->get();
		$data["menu"] 			= "admin";
		$data["content"]		= $this->load->view("grid_master_tanya",$data,true);
		$this->load->view('main',$data);
	}

	public function pesan()
	{
		$data["master_tanya"]	= $this->m_tanya_model->get();
		$data["menu"] 			= "admin";
		$data["message"] 		= "pesan";
		$data["content"]		= $this->load->view("grid_master_tanya",$data,true);
		$this->load->view('main',$data);
	}

	public function error()
	{
		$data["master_tanya"]	= $this->m_tanya_model->get();
		$data["menu"] 			= "admin";
		$data["message"] 		= "error";
		$data["content"]		= $this->load->view("grid_master_tanya",$data,true);
		$this->load->view('main',$data);
	}

	public function form($id=FALSE){
		$this->load->model("m_ijin_model");

		$this->form_validation->set_rules("urutan","Urutan","required");
		$this->form_validation->set_rules("pertanyaan","Pertanyaan","required");
		$this->form_validation->set_rules("jawaban","Jawaban","required");
		if($id == TRUE){
			$value	= $this->m_tanya_model->get($id);
			if ($value["JAWABAN2"] != null) { ($this->form_validation->set_rules("jawaban2","Jawaban","required"));}
			if ($value["JAWABAN3"] != null) { ($this->form_validation->set_rules("jawaban3","Jawaban","required"));}
			if ($value["JAWABAN4"] != null) { ($this->form_validation->set_rules("jawaban4","Jawaban","required"));}
		}
		$this->form_validation->set_rules("status","Status","required");
		$this->form_validation->set_rules("tampak","Tampak","required");

		if($this->form_validation->run() == FALSE){
			if($id==FALSE){
				$data["id"]			= $id;
				$data["master_ijin"]= $this->m_ijin_model->get_by(array("STATUS"=>1));
				$data["content"]	= $this->load->view("form_master_tanya",$data,true);
				$data["menu"] 		= "admin";
				$this->load->view("main",$data);
			} else {
				$data["id"]			= $id;
				$data["master_ijin"]= $this->m_ijin_model->get_join_by2($id,FALSE,array("master_ijin.ID_IJIN","dt_pertanyaan.ID_IJIN AS ID_IJIN2","NAMA_IJIN"));
				$data["value"]		= $this->m_tanya_model->get($id);
				$data["content"]	= $this->load->view("form_master_tanya",$data,true);
				$data["menu"] 		= "admin";
				$this->load->view("main",$data);
			}
		} else {
			$this->load->model("dt_tanya_model");
			$master_ijin= $this->m_ijin_model->get_by(array("STATUS"=>1));
			// $id         = $this->input->post("idpertanyaan");
			$urutan		= $this->input->post("urutan");
			$pertanyaan	= $this->input->post("pertanyaan");
			// $bobot		= $this->input->post("bobot");
			$jawaban	= $this->input->post("jawaban");
			$jawaban2	= ($this->input->post("jawaban2") == null) ? (null) : ($this->input->post("jawaban2"));
			$jawaban3	= ($this->input->post("jawaban3") == null) ? (null) : ($this->input->post("jawaban3"));
			$jawaban4	= ($this->input->post("jawaban4") == null) ? (null) : ($this->input->post("jawaban4"));
			$status		= $this->input->post("status");
			$tampak		= $this->input->post("tampak");

			if( $id == FALSE ) {
				$validasi_cek = $this->m_tanya_model->get_by(array('URUTAN' => $urutan, 'PERTANYAAN' => $pertanyaan), NULL, TRUE);
				if( $validasi_cek == NULL ) {
					$data = array("PERTANYAAN"=>$pertanyaan,"URUTAN"=>$urutan,"JAWABAN"=>$jawaban,"JAWABAN2"=>$jawaban2,"JAWABAN3"=>$jawaban3,"JAWABAN4"=>$jawaban4,"STATUS"=>$status,"TAMPAK"=>$tampak);
					$id_pertanyaan = $this->m_tanya_model->save($data);

					foreach( $master_ijin as $row ) {
						if( $this->input->post($row["ID_IJIN"]) != NULL ) {
							$data_detail = array("ID_PERTANYAAN"=>$id_pertanyaan,"ID_IJIN"=>$row["ID_IJIN"]);
							$this->dt_tanya_model->save($data_detail);
						}
					}
					redirect("master_pertanyaan/pesan");
				} else {
					redirect("master_pertanyaan/error");
				}
			} else {
				$cek_urutan 	= $this->m_tanya_model->get_by('URUTAN',$urutan,NULL,TRUE);
				$cek_pertanyaan = $this->m_tanya_model->get_by('PERTANYAAN',$pertanyaan,NULL,TRUE);

				if( $cek_urutan != NULL && $cek_pertanyaan != NULL && $cek_urutan['ID_PERTANYAAN'] == $cek_pertanyaan['ID_PERTANYAAN'] ) {
					// urutan dan pertanyaan sama tapi field yang lain berubah - edit
					$this->dt_tanya_model->delete_by("ID_PERTANYAAN",$id);

					$data = array("PERTANYAAN"=>$pertanyaan,"URUTAN"=>$urutan,"JAWABAN"=>$jawaban,"JAWABAN2"=>$jawaban2,"JAWABAN3"=>$jawaban3,"JAWABAN4"=>$jawaban4,"STATUS"=>$status,"TAMPAK"=>$tampak);
					$this->m_tanya_model->save($data,$id);

					foreach($master_ijin as $row){
						if($this->input->post($row["ID_IJIN"]) != NULL) {
							$data_detail = array("ID_PERTANYAAN"=>$id,"ID_IJIN"=>$row["ID_IJIN"]);
							$this->dt_tanya_model->save($data_detail);
						}
					}
					redirect("master_pertanyaan/pesan");
				} elseif( $cek_urutan != NULL && $cek_pertanyaan == NULL && $cek_urutan['ID_PERTANYAAN'] == $id ) {
					// urutan sama tapi pertanyaan beda + field yang lain beda - edit
					$this->dt_tanya_model->delete_by("ID_PERTANYAAN",$id);

					$data = array("PERTANYAAN"=>$pertanyaan,"URUTAN"=>$urutan,"JAWABAN"=>$jawaban,"JAWABAN2"=>$jawaban2,"JAWABAN3"=>$jawaban3,"JAWABAN4"=>$jawaban4,"STATUS"=>$status,"TAMPAK"=>$tampak);
					$this->m_tanya_model->save($data,$id);

					foreach($master_ijin as $row){
						if($this->input->post($row["ID_IJIN"]) != NULL) {
							$data_detail = array("ID_PERTANYAAN"=>$id,"ID_IJIN"=>$row["ID_IJIN"]);
							$this->dt_tanya_model->save($data_detail);
						}
					}
					redirect("master_pertanyaan/pesan");

				} elseif( $cek_urutan == NULL && $cek_pertanyaan != NULL && $cek_pertanyaan['ID_PERTANYAAN'] == $id ) {
					// pertanyaan sama tapi urutan + field yang lain beda - edit
					$this->dt_tanya_model->delete_by("ID_PERTANYAAN",$id);
					$data = array("PERTANYAAN"=>$pertanyaan,"URUTAN"=>$urutan,"JAWABAN"=>$jawaban,"JAWABAN2"=>$jawaban2,"JAWABAN3"=>$jawaban3,"JAWABAN4"=>$jawaban4,"STATUS"=>$status,"TAMPAK"=>$tampak);
					$this->m_tanya_model->save($data,$id);

					foreach($master_ijin as $row){
						if($this->input->post($row["ID_IJIN"]) != NULL) {
							$data_detail = array("ID_PERTANYAAN"=>$id,"ID_IJIN"=>$row["ID_IJIN"]);
							$this->dt_tanya_model->save($data_detail);
						}
					}
					redirect("master_pertanyaan/pesan");
				} else {
					redirect("master_pertanyaan/error");
				}
			}
		}
	}
}